import { source } from '../services/component';
import { support,organization,difficulty,isOk } from '../services/dysupport'
import { particulars } from '../services/Personalinformation'
import {Toast} from 'antd-mobile'
import {routerRedux} from 'dva/router';
const empty = require('is-empty');
export default {

  namespace: 'supportList',

  state: {

  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({pathname, query}) => {
        if (pathname.toString() == "/work/dysupportxx") {
          dispatch({type: 'audit'});
          dispatch({type: 'readyPass'});
          dispatch({type: 'memberData'});
          dispatch({type: 'difficultyType'});
          dispatch({type: 'isSubmit'});
        }
      })
    }
  },

  effects: {
    //帮扶列表
    *audit({payload}, { call, put, select }) {
      const {data} = yield call(support, {relation:["org"]});
       const data1= data.ret;
      yield put({
        type: 'success',
        payload: {
          data1:data1
        }
      })
    },

    //新增帮扶组织信息
    *readyPass({payload}, { call, put, select }) {
      const data2 = yield call(organization, payload);
      const data3 = data2.data.ret;
      const datalist=[];
      for (let i in data3){
        datalist.push(data3[i])
      }
      yield put({
        type: 'success',
        payload: {
          dataList:datalist
        }
      })
    },

    //新增帮扶党员信息
    *memberData({payload}, { call, put, select }) {
      if(payload){
        const data4 = yield call(particulars, {org_code:"500"});
        const data5=data4.data.ret;
        const datalist1=[];
        if (!empty(data5)){
          data5.map(item=>{
              datalist1.push({label:item.name,value:item.code})
          })
        }
        yield put({
          type: 'success',
          payload: {
            member_data:datalist1
          }
        });
        payload.resolve(datalist1)
      }



      // const data5 = data4.data.ret;
      // const datalist1=[];
      // const member_data=[];
      // if(!empty(data5)){
      //   data5.map(item =>{
      //     if(!empty(item.member)){
      //       datalist1.push(item.member)
      //     }
      //   })
      // }
      // console.log(datalist1,'datalist1')
      //   for (let i in datalist1){
      //     member_data.push({label:datalist1[i].name,value:datalist1[i].code})
      //   }
      //
      // console.log(member_data,"member_data")

    },
//困难类型
    *difficultyType({payload}, { call, put, select }) {
      const data6 = yield call(difficulty, {table:"dict_difficult_type"});

      const data7= data6.data.ret;
      const datalist2=[];
      data7.map(item=>{
        datalist2.push({label:item.name,value:item.code})
      })
      yield put({
        type: 'success',
        payload: {
          dType:datalist2
        }
      })
    },

    //提交
    *isSubmit({payload}, { call, put, select }) {
      const ok = yield call(isOk, payload);
      console.log(ok, "ok");
      if (ok.data.code == 200) {
        Toast.success('添加成功！', 1);
        yield put(
          routerRedux.push({
            pathname: "/work/dysupportxx",
          })
        )
      }
    }
  },

  reducers: {
    success(state, action){
      return {...state, ...action.payload};
    }
  }
};
